#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace FormaBedoboNS
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class MessageScreen : MenuScreen
    {
        #region Fields
        
        const int hPad = 32;
        const int vPad = 16;

        MenuEntry rendbenMenuEntry;
        MenuEntry megsemMenuEntry;
        Texture2D gradientTexture;
        string message;

        #endregion

        public event EventHandler<PlayerIndexEventArgs> Accepted;
        public event EventHandler<PlayerIndexEventArgs> Cancelled;

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public MessageScreen(string msg)
            : base("",false)
        {
            this.message = msg;
            // Create our menu entries.
            rendbenMenuEntry = new MenuEntry(string.Empty);
            megsemMenuEntry = new MenuEntry(string.Empty);

            SetMenuEntryText();

            // Hook up menu event handlers.
            rendbenMenuEntry.Selected += rendbenMenuEntrySelected;
            megsemMenuEntry.Selected += megsemMenuEntrySelected;

            MenuEntries.Add(rendbenMenuEntry);
            MenuEntries.Add(megsemMenuEntry);

            //Program.game.IsMouseVisible = true;
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            rendbenMenuEntry.Text = AppSettings.getString("rendben");
            megsemMenuEntry.Text = AppSettings.getString("megsem");

        }

        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                ContentManager content = ScreenManager.Game.Content;
                gradientTexture = content.Load<Texture2D>("gradient");
            }
            base.Activate(true);
        }
        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the rendben menu entry is selected.
        /// </summary>
        void rendbenMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            if (Accepted != null)
                Accepted(this, new PlayerIndexEventArgs(e.PlayerIndex));
            ExitScreen();
        }

        void megsemMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            if (Cancelled != null)
                Cancelled(this, new PlayerIndexEventArgs(e.PlayerIndex));
            ExitScreen();
        }


        #endregion

        private Rectangle calculateRect(ref Vector2 position)
        {
            position = new Vector2(0f, ScreenManager.GraphicsDevice.Viewport.Height / 2 - vPad);
            
            int sz = base.GetMaxWidth();
            if ((int)FormaBedoboGame.screenManager.Font.MeasureString(message).X>sz)
                sz = (int)FormaBedoboGame.screenManager.Font.MeasureString(message).X;

            position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - sz / 2 - hPad;

            Rectangle backgroundRectangle = new Rectangle((int)position.X,
                                                          (int)position.Y - (int)FormaBedoboGame.screenManager.Font.MeasureString(message).Y - vPad,
                                                          sz + hPad * 2,
                                                          (int)base.GetHegihtSum() + (int)FormaBedoboGame.screenManager.Font.MeasureString(message).Y + 2*vPad);
            

            return backgroundRectangle;
        }

        public override void Draw(GameTime gameTime)
        {
            // Fade the popup alpha during transitions.
            Color color = Color.White * TransitionAlpha;
            
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();

            Vector2 position= new Vector2();
            // Draw the background rectangle.
            spriteBatch.Draw(gradientTexture, calculateRect(ref position), color);

            // Draw the message box text.
            position.X += hPad;
            position.Y -= (int)FormaBedoboGame.screenManager.Font.MeasureString(message).Y;
            spriteBatch.DrawString(ScreenManager.Font, message, position, color);

            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}
